package com.heima.service.impl;

import com.heima.mapper.RoleMapper;
import com.heima.mapper.UserMapper;
import com.heima.pojo.Role;
import com.heima.pojo.User;
import com.heima.pojo.dto.UserAddDTO;
import com.heima.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    UserMapper userMapper;

    /**
     * 角色列表
     */
    @Override
    public List<Role> list() {
        List<Role> roles = roleMapper.selectList();
        return roles;
    }

    /**
     * 新增人员
     */
    @Override
    public boolean add(UserAddDTO dto) {
        User user = new User();
        BeanUtils.copyProperties(dto, user);
        user.setSecret(null);
        user.setLoginName(null);
        user.setPassword(null);
        if (dto.getStatus()){
            user.setStatus(1);
        }else {
            user.setStatus(0);
        }
        Role role = roleMapper.roleById(dto.getRoleId());
        user.setRoleCode(role.getRoleCode());
        int result = userMapper.add(user);
        return result > 0;
    }
}
